using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public static class  Until
{
    private static Hashtable ht=new Hashtable();

    static char[] TrimChar = { ' ', '-', '\\', '=', '[', ']' };
    /// <summary>
    /// 判断连个float类型是否相等
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public static bool FloatEqual(float a, float b)
    {
        return Mathf.Abs(a - b) < 0.0000001f;
    }

    

    /// <summary>
    /// 世界坐标转UI屏幕坐标
    /// </summary>
    /// <param name="wordPosition"></param>
    /// <returns></returns>
    public static Vector2 WordToScenePoint(Vector3 wordPosition)
    {
        CanvasScaler canvasScaler = GameObject.Find("Canvas").GetComponent<CanvasScaler>();

        float resolutionX = canvasScaler.referenceResolution.x;

        float resolutionY = canvasScaler.referenceResolution.y;

        float offect = (Screen.width / canvasScaler.referenceResolution.x) * (1 - canvasScaler.matchWidthOrHeight) + (Screen.height / canvasScaler.referenceResolution.y) * canvasScaler.matchWidthOrHeight;

        Vector2 a = RectTransformUtility.WorldToScreenPoint(Camera.main, wordPosition);
        Debug.LogWarning(a);
        Vector2 screenSize = new Vector2(Screen.width, Screen.height);
        a -= screenSize / 2;//将屏幕坐标变换为以屏幕中心为原点
        Vector2 anchorPos = a / screenSize * GameObject.Find("Canvas").GetComponent<RectTransform>().sizeDelta;//缩放得到UGUI坐标
        Debug.LogWarning(anchorPos);

        return new Vector2(a.x / offect, a.y / offect); ;
    }


    public static Vector2 ScenePointToWord(Vector3 wordPosition)
    {
        CanvasScaler canvasScaler = GameObject.Find("Canvas").GetComponent<CanvasScaler>();

        float resolutionX = canvasScaler.referenceResolution.x;

        float resolutionY = canvasScaler.referenceResolution.y;

        float offect = (Screen.width / canvasScaler.referenceResolution.x) * (1 - canvasScaler.matchWidthOrHeight) + (Screen.height / canvasScaler.referenceResolution.y) * canvasScaler.matchWidthOrHeight;
        wordPosition.x *= offect;
        wordPosition.y *= offect;
        Vector2 a = Camera.main.ScreenToWorldPoint(wordPosition);

        return a;

    }
    /// <summary>
    /// 反射创建类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name"></param>
    /// <returns></returns>
    public static T CreateObject<T>(string name) where T : class
    {
        if (ht.Contains(name))
        {
            //Debug.Log(name);
            return ht[name] as T;
        }


        Type type = Type.GetType(name);
        if (type == null)
        {
            Debug.LogError("提取的类找不到，请检查。 path=" + name);
            return null;
        }
        else
        {
            ht.Add(name, Activator.CreateInstance(type) as T);
            //Debug.Log("创建反射" + name);
            return ht[name] as T;
        }

    }
    /// <summary>
    /// 反射获取静态类参数
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static PropertyInfo GetPropertyValue(this object obj, string name)
    {
        if (ht.Contains(name))
        {
            //Debug.Log(name);
            return ht[name] as PropertyInfo;
        }
        PropertyInfo propertyInfo= obj.GetType().GetProperty(name);
        if (propertyInfo == null)
        {           
            return null;
        }
        else
        {
            ht.Add(name, propertyInfo);
            //Debug.Log("创建反射" + name);
            return ht[name] as PropertyInfo;
        }
    }
    /// <summary>
    /// 反射获取普通类参数
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static FieldInfo GetFieldValue(this object obj, string name)
    {
        if (ht.Contains(name))
        {
            //Debug.Log(name);
            return ht[name] as FieldInfo;
        }
        FieldInfo propertyInfo = obj.GetType().GetField(name);
        if (propertyInfo == null)
        {
            return null;
        }
        else
        {
            ht.Add(name, propertyInfo);
            //Debug.Log("创建反射" + name);
            return ht[name] as FieldInfo;
        }
    }

   
    /// <summary>
    /// 自动生成导入资源的枚举和路径
    /// </summary>
    /// <param name="writeName">写入的名字</param>
    /// <param name="prefix">前缀</param>
    /// <param name="_typeForPanelPath">cs类型路径</param>
    /// <param name="_jsonForPanelPath">json路径</param>
    public static void WriteTypeAndPath(string writeName,string prefix, string _typeForPanelPath= "Assets/GameFramework/Resource/ResourcesType.cs", string _jsonForPanelPath= "Assets/GameFramework/Resources/ResourcesType.json")
    {
        writeName = writeName.RemoveStringExt().Replace("Assets/Resources/", "");
        string[] strarry = writeName.Split('/');
        //判断是否存在类型文件 如果存在读取文件内容并添加写入 类型枚举中
        if (IOUntil.GetFileSuccess(_typeForPanelPath))
        {

            //写入内容
            StringBuilder fileContent = new StringBuilder();
            fileContent.Append(IOUntil.GetFileText(_typeForPanelPath));

            if (!fileContent.ToString().Contains(prefix + strarry[strarry.Length - 1]))
            {
                //检测writeName是否含有空格 或者/
                
                fileContent = fileContent.Replace("/*枚举类型*/", ",\n\t" + prefix+strarry[strarry.Length-1].Replace(" ","").Replace("[","").Trim(TrimChar) + "/*枚举类型*/");
                IOUntil.CreateTextFile(_typeForPanelPath, fileContent.ToString());
            }



        }
        else
        {
            Debug.Log("Type不存在");
        }

        if (IOUntil.GetFileSuccess(_jsonForPanelPath))
        {

            //写入内容
            StringBuilder fileContent = new StringBuilder();
            fileContent.Append(IOUntil.GetFileText(_jsonForPanelPath));

            var temp = ",\n\t\n\t    {\"" + "resourcesTypeString" + "\":" + "\"" + prefix + strarry[strarry.Length - 1].Replace(" ", "").Replace("[", "").Trim(TrimChar) + "\",\n\t" + "\"" + "path" + "\":" + "\"" + writeName + "\"}";

            if (!fileContent.ToString().Contains(temp))
            {
                fileContent = fileContent.Replace("/**/", temp + "/**/");

                IOUntil.CreateTextFile(_jsonForPanelPath, fileContent.ToString());
            }


        }
        else
        {
            Debug.Log("Json不存在");
        }
    }

    /// <summary>
    /// 将创建的Prefab的路径和名称从对应的Json文件和路径中删除
    /// </summary>
    /// <param name="writeName">写入的名字</param>
    /// <param name="prefix">前缀</param>
    /// <param name="_typeForPanelPath">cs类型路径</param>
    /// <param name="_jsonForPanelPath">json路径</param>
    public static void DeleteTypeAndPath(string writeName, string prefix, string _typeForPanelPath = "Assets/GameFramework/Resource/ResourcesType.cs", string _jsonForPanelPath = "Assets/GameFramework/Resources/ResourcesType.json")
    {
        writeName = writeName.RemoveStringExt().Replace("Assets/Resources/", "");
        string[] strarry = writeName.Split('/');
        //判断是否存在类型文件 如果存在读取文件内容并添加写入 类型枚举中
        if (IOUntil.GetFileSuccess(_typeForPanelPath))
        {

            //删除内容
            StringBuilder fileContent = new StringBuilder();
            fileContent.Append(IOUntil.GetFileText(_typeForPanelPath));
            //Debug.Log(fileContent.ToString());
            if (!fileContent.ToString().Contains(strarry[strarry.Length - 1]))
            {
                //检测writeName是否含有空格 或者/

                fileContent = fileContent.Replace(",\n\t" + prefix + strarry[strarry.Length - 1],"");
                IOUntil.CreateTextFile(_typeForPanelPath, fileContent.ToString());
            }           
            else
            {
                Debug.Log("不存在" + prefix + strarry[strarry.Length - 1]);

            }


        }
        else
        {
            Debug.Log("Type不存在");
        }

        if (IOUntil.GetFileSuccess(_jsonForPanelPath))
        {

            //写入内容
            StringBuilder fileContent = new StringBuilder();
            fileContent.Append(IOUntil.GetFileText(_jsonForPanelPath));

            //var temp = ",\n\t\n\t    {\"" + "panelTypeString" + "\":" + "\"" + writeName + "\",\n\t" + "\"" + "path" + "\":" + "\"UIPanel/" + writeName + "Panel" + "\"}";
            var temp = ",\n\t\n\t    {\"" + "resourcesTypeString" + "\":" + "\"" + prefix + strarry[strarry.Length - 1] + "\",\n\t" + "\"" + "path" + "\":" + "\"" + writeName.GetBackOtherStr(4) + "\"}";

            if (fileContent.ToString().Contains(temp))
            {
                fileContent = fileContent.Replace(temp, "");

                IOUntil.CreateTextFile(_jsonForPanelPath, fileContent.ToString());
            }


        }
        else
        {
            Debug.Log("Json不存在");
        }
    }

   

}


//using UnityEngine;
//using System.Collections;
//using System.IO;
//using System.Security;  
//using System.Security.Cryptography;  
//using System.Text;  

///// <summary>
///// 游戏架构
///// </summary>
//namespace GameFrame {



//	/// <summary>
//	/// 公用基础函数库
//	/// <remarks>作者: YangYxd</remarks>
//	/// </summary>
//	public static class Common {
//		private const long Jan1st1970Ms = 621355968000000000L; //DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc).Ticks;
//		private const long Jan1st1970Ms_U8 = Jan1st1970Ms + 288000000000L;


//		public delegate bool CondDelegate(); 

//		/// <summary>
//		/// 等待指定的毫秒
//		/// </summary>
//		public static IEnumerator WaitForMillis(long millis, CondDelegate cond = null) { 
//			long s = Common.Ticks;
//			while(true) { 
//				if (Common.Ticks - s < millis && (cond == null || !cond())) { 
//					yield return null; 
//				} else { 
//					break; 
//				} 
//			} 
//		} 

//	    /// <summary>
//	    /// 整型转为字符串
//	    /// </summary>
//		public static string intToStr(int v) {
//	        return v.ToString();
//		}

//	    /// <summary>
//	    /// 字符串转为整型
//	    /// </summary>
//	    public static int strToInt(string v, int defaultValue = 0) {
//	        int i = defaultValue;
//	        int.TryParse(v, out i);
//	        return i;
//	    }

//		/// <summary>
//		/// 返回当前时间的毫秒数, 这个毫秒其实就是自1970年1月1日0时起的毫秒数
//		/// </summary>
//		public static long currentTimeMillis() {		
//			return (System.DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000;
//		}

//		/// <summary>
//		/// 返回指定时间的毫秒数, 这个毫秒是自1970年1月1日0时起的毫秒数
//		/// </summary>
//		public static long getTimeInMillis(System.DateTime value) {		
//			return (value.Ticks - Jan1st1970Ms) / 10000;
//		}

//		/// <summary>
//		/// 从一个代表自1970年1月1日0时起的毫秒数，转换为DateTime (北京时间)
//		/// </summary>
//		public static System.DateTime getDateTime(long timeMillis) {
//			return new System.DateTime (timeMillis * 10000 + Jan1st1970Ms_U8);
//		}

//		/// <summary>
//		/// 从一个代表自1970年1月1日0时起的毫秒数，转换为DateTime (UTC时间)
//		/// </summary>
//		public static System.DateTime getDateTimeUTC(long timeMillis) {
//			return new System.DateTime (timeMillis * 10000 + Jan1st1970Ms);
//		}

//		/// <summary>
//		/// 返回一个以毫秒为单位的时间 （0001-01-01 00:00:00.000 算起）
//		/// </summary>
//		public static long Ticks {
//			get { 
//				return (long) (System.DateTime.UtcNow.Ticks * 0.0001f)	;
//			}
//		}

//		/// <summary>
//		/// 是否是触摸设备
//		/// </summary>
//		public static bool isTouchDevice {
//			get {
//				#if UNITY_EDITOR
//				return false;
//				#else
//				#if UNITY_IPHONE || UNITY_ANDROID
//				return true;
//				#else
//				return false;
//				#endif
//				#endif
//			}
//		}


//	    /// <summary>
//	    /// 平台对应文件夹
//	    /// </summary>
//	    /// <returns></returns>
//	    public static string getRuntimePlatform() {
//	        string platform = "";
//	        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) {
//	            platform = "Windows";
//	        } else if (Application.platform == RuntimePlatform.Android) {
//	            platform = "Android";
//	        } else if (Application.platform == RuntimePlatform.IPhonePlayer) {
//	            platform = "IOS";
//	        } else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor) {
//	            platform = "OSX";
//	        }
//	        return platform;
//	    }

//	    /// <summary>
//	    /// 根据指定的资源类型，查找游戏资源
//	    /// </summary>
//	    /// <returns>返回资源对象</returns>
//	    /// <param name="name">资源名称</param>
//	    public static T findRes<T>(string name) where T : Object {
//			T[] objs = Resources.FindObjectsOfTypeAll<T>();
//			if (objs != null && objs.Length > 0) {
//				foreach (Object obj in objs) {
//					if (obj.name == name)
//						return obj as T;
//				}
//			}
//			objs = AssetBundle.FindObjectsOfType<T>();
//			if (objs != null && objs.Length > 0) {
//				foreach (Object obj in objs) {
//					if (obj.name == name)
//						return obj as T;
//				}
//			}
//			return default(T);
//		}

//	    /// <summary>
//	    /// 获取文件扩展名
//	    /// </summary>
//	    /// <param name="file">文件名称（可以带路径）.</param>
//	    public static string getFileExt(string file) {
//	        if (file == null || file.Length == 0)
//	            return "";
//	        int i = file.LastIndexOf('.');
//	        if (i < 0) return "";
//	        return file.Substring(i + 1);
//	    }

//	    /// <summary>
//	    /// 获取文件名称（包含扩展名）
//	    /// </summary>
//	    /// <param name="file">文件名称或完整路径</param>
//	    public static string getFileName(string file) {
//			return getFileName (file, true);
//	    }

//		/// <summary>
//		/// 获取文件名称
//		/// </summary>
//		/// <param name="file">文件名称或完整路径</param>
//		/// <param name="isHaveExt">是否包含有文件扩展名</param>
//		public static string getFileName(string file, bool isHaveExt) {
//			if (file == null || file.Length == 0)
//				return "";
//			int i = file.LastIndexOf('/');
//			if (i < 0) {
//				if (!isHaveExt) {
//					int j = file.LastIndexOf ('.');
//					if (j >= 0)
//						return file.Substring (0, j);
//				}
//				return file;
//			} else {
//				if (isHaveExt)
//					return file.Substring (i + 1);
//				else {
//					int j = file.LastIndexOf ('.');
//					if (j > i)
//						return file.Substring (i + 1, j - i - 1);
//					else
//						return file.Substring (i + 1);
//				}
//			}
//		}



//	    /// <summary>
//	    /// 获取文件路径
//	    /// </summary>
//	    /// <param name="file">文件完整路径</param>
//	    public static string getFilePath(string file) {
//	        if (file == null || file.Length == 0)
//	            return "";
//	        return Path.GetDirectoryName(file);
//	    }

//	    /// <summary>
//	    /// 创建文件夹（如果文件夹已经存在，则不再创建）
//	    /// </summary>
//	    public static bool createFolder(string path) {
//	        if (path == null || path.Length == 0 || Directory.Exists(path))
//	            return false;
//	        Directory.CreateDirectory(path);
//	        return true;
//	    }

//	    /// <summary>
//	    /// 检测文件夹是否存在
//	    /// </summary>
//	    public static bool existsPath(string path) {
//	        if (path == null || path.Length == 0)
//	            return false;
//	        else
//	            return Directory.Exists(path);
//	    }

//	    /// <summary>
//	    /// 检测文件是否存在
//	    /// </summary>
//	    public static bool existsFile(string file) {
//	        if (file == null || file.Length == 0)
//	            return false;
//	        else
//	            return File.Exists(file);
//	    }

//	    /// <summary>
//	    /// 删除文件
//	    /// </summary>
//	    public static bool deleteFile(string file) {
//	        if (!existsFile(file))
//	            return true;
//	        try {
//	            File.Delete(file);
//	            return true;
//	        } catch (System.Exception e) {
//	            Debug.LogError(e.Message);
//	            return false;
//	        }
//	    }

//	    /// <summary>
//	    /// 删除文件夹
//	    /// <param name="recursive">是否删除所有子文件夹和文件</param>
//	    /// </summary>
//	    public static bool deleteFolder(string path, bool recursive) {
//	        if (path == null || path.Length == 0)
//	            return true;
//	        try {
//	            DirectoryInfo dir = new DirectoryInfo(path);
//	            dir.Delete(recursive);
//	            return true;
//	        } catch (System.Exception e) {
//	            Debug.LogError(e.Message);
//	            return false;
//	        }
//	    }

//	    /// <summary>
//	    /// 返回URL形式的streamingAssets地址（自动兼容平台）
//	    /// </summary>
//	    public static string streamingAssetsURL {
//	        get {
//				#if UNITY_ANDROID && !UNITY_EDITOR
//				return "jar:file://" + Application.dataPath + "!/assets/";
//				#elif UNITY_IPHONE
//				return Application.dataPath + "/Raw/";
//				#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
//	            return "file://" + Application.dataPath + "/StreamingAssets/";
//				#endif
//	        }
//	    }

//		/// <summary>
//		/// 获取当前透视摄像机的视口区域
//		/// </summary>
//		public static Vector3[] getCameraCorners (float distance) {		
//			Camera theCamera = Camera.main;
//			Transform tx = theCamera.transform;
//			Vector3[] corners = new Vector3[ 4 ];

//			float halfFOV = ( theCamera.fieldOfView * 0.5f ) * Mathf.Deg2Rad;
//			float aspect = theCamera.aspect;

//			float height = distance * Mathf.Tan( halfFOV );
//			float width = height * aspect;

//			// UpperLeft
//			corners[ 0 ] = tx.position - ( tx.right * width );
//			corners[ 0 ] += tx.up * height;
//			corners[ 0 ] += tx.forward * distance;

//			// UpperRight
//			corners[ 1 ] = tx.position + ( tx.right * width );
//			corners[ 1 ] += tx.up * height;
//			corners[ 1 ] += tx.forward * distance;

//			// LowerLeft
//			corners[ 2 ] = tx.position - ( tx.right * width );
//			corners[ 2 ] -= tx.up * height;
//			corners[ 2 ] += tx.forward * distance;

//			// LowerRight
//			corners[ 3 ] = tx.position + ( tx.right * width );
//			corners[ 3 ] -= tx.up * height;
//			corners[ 3 ] += tx.forward * distance;

//			return corners;
//		}

//		/// <summary>
//		/// 获取使用正交相机时，主摄像机在地图上的移动区域。
//		/// 使用正交相机时，可以通过旋转摄像机X轴来显示地图，此时通过Y轴控制摄像机高度，X控制水平位置，Z轴控制上下位置。
//		/// </summary>
//		/// <returns>返回摄像机移动区域.</returns>
//		/// <param name="RotationX">摄像机旋转角度(X轴)</param>
//		/// <param name="CameraSize">相机大小</param>
//		/// <param name="CamersY">摄像机高度(Y轴坐标)</param>
//		/// <param name="MapW">地图宽度</param>
//		/// <param name="MapH">地图高度</param>
//		public static Rect OrthographicCameraEdge(float RotationX, float CameraSize, float CamersY, float MapW, float MapH) {
//			float CXSize = CameraSize * 2 * ((float) Screen.width / (float) Screen.height);
//			float CYSize = CameraSize * 2 / Mathf.Cos ( RotationX * Mathf.Deg2Rad );
//			return new Rect (
//				CXSize / 2.0f, 
//				CYSize / 2.0f - CamersY, 
//				MapW - CXSize, 
//				MapH - CYSize);;
//		}

//		/// <summary>
//		/// 显示一个提示信息 Toast
//		/// </summary>
//		public static void Hint(string msg) {
//			#if UNITY_ANDROID || UNITY_ANDROID_API
//			AndroidCommon.Hint(msg);
//			#else
//			#endif
//		}
//	}
//}
